package net.csdn.business.notify.common.enums;


import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.ToString;
import net.csdn.business.notify.common.pipeline.model.ContentModel;
import net.csdn.business.notify.common.pipeline.model.EmailContentModel;
import net.csdn.business.notify.common.pipeline.model.ImContentModel;
import net.csdn.business.notify.common.pipeline.model.SmsContentModel;

import java.util.Arrays;
import java.util.Objects;

/**
 * @ClassName ChannelType
 * @Description 通知渠道枚举
 * @Author zhangyl
 * @Date 2023/11/29 17:55
 */
@Getter
@ToString
@AllArgsConstructor
public enum NotifyChannel {


    /**
     * IM(站内信)  -- 未实现该渠道
     */
    IM(10, "IM(站内信)", ImContentModel.class, "im", null, null),
    /**
     * email(邮件) -- QQ、163邮箱
     */
    EMAIL(20, "email(邮件)", EmailContentModel.class, "email", null, null),

    /**
     * sms(短信)  -- 腾讯云、云片
     */
    SMS(30, "sms(短信)", SmsContentModel.class, "sms", null, null),
    ;

    /**
     * 编码值
     */
    private final Integer code;

    /**
     * 描述
     */
    private final String description;

    /**
     * 内容模型Class
     */
    private final Class<? extends ContentModel> contentModelClass;

    /**
     * 英文标识
     */
    private final String codeEn;

    /**
     * accessToken prefix
     */
    private final String accessTokenPrefix;

    /**
     * accessToken expire
     * 单位秒
     */
    private final Long accessTokenExpire;

    /**
     * 通过code获取class
     *
     * @param code
     * @return
     */
    public static Class<? extends ContentModel> getChanelModelClassByCode(Integer code) {
        return Arrays.stream(values()).filter(channelType -> Objects.equals(code, channelType.getCode()))
                .map(NotifyChannel::getContentModelClass)
                .findFirst().orElse(null);
    }


    public static String ofCodeEn(Integer code) {
        for (NotifyChannel type : NotifyChannel.values()) {
            if (type.code.equals(code)) {
                return type.codeEn;
            }
        }
        throw new IllegalArgumentException("ChannelType get codeEn param error");
    }


    public static NotifyChannel getChannelType(Integer code) {
        for (NotifyChannel type : NotifyChannel.values()) {
            if (type.code.equals(code)) {
                return type;
            }
        }
        throw new IllegalArgumentException("ChannelType getChannelType param error");
    }
}
